#include "module.h"
#include "FreeRTOSConfig.h"
#include "FreeRTOS.h"
#include "task.h"
uint8_t ms_count = 0;
uint16_t ms5_count = 0;
uint8_t ms10_count = 0;
uint8_t ms20_count = 0;
uint8_t ms_camera_count = 0;
uint16_t ms300_count = 0;
uint16_t ms500_count = 0;

volatile unsigned long long FreeRTOSRunTimeTicks;

void ConfigureTimeForRunTimeStats(void)
{
	//定时器5频率为84M/84=1M，自动重装载为50-1，那么定时器周期就是50us
	FreeRTOSRunTimeTicks=0;
//	TIM5_Init(49);	//初始化TIM5
}

void BSP_TIM2_Callback(void)
{
	ms_count++;
	ms5_count++;
	ms10_count++;
	ms_camera_count++;
	ms20_count++;
	ms300_count++;
	ms500_count++;
}

void BSP_TIM5_Callback(void)
{
	FreeRTOSRunTimeTicks++;
}


extern void xPortSysTickHandler(void);
void SysTick_Handler(void)
{
    if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED)
    {
        xPortSysTickHandler();
    }
		HAL_IncTick();
}

void MC_CANFilterInit(void)
{
    FilterUnit can1Filter[2] = {0};
    FilterUnit can2Filter[2] = {0};
    /* 主控CAN1过滤器初始化 */
    can1Filter[0].mode              = MODE_LIST16;
    can1Filter[0].FilterFIFO        = CAN_FILTER_FIFO0;
    can1Filter[0].reg.list16.IDE1   = CAN_ID_STD;
    can1Filter[0].reg.list16.RTR1   = CAN_RTR_DATA;
    can1Filter[0].reg.list16.StdID1 = 0x201;
    can1Filter[0].reg.list16.IDE2   = CAN_ID_STD;
    can1Filter[0].reg.list16.RTR2   = CAN_RTR_DATA;
    can1Filter[0].reg.list16.StdID2 = 0x202;
    can1Filter[0].reg.list16.IDE3   = CAN_ID_STD;
    can1Filter[0].reg.list16.RTR3   = CAN_RTR_DATA;
    can1Filter[0].reg.list16.StdID3 = 0x203;
    can1Filter[0].reg.list16.IDE4   = CAN_ID_STD;
    can1Filter[0].reg.list16.RTR4   = CAN_RTR_DATA;
    can1Filter[0].reg.list16.StdID4 = 0x205;
    
//    can1Filter[1].mode              = MODE_LIST16;
//    can1Filter[1].FilterFIFO        = CAN_FILTER_FIFO0;
//    can1Filter[1].reg.list16.IDE1   = CAN_ID_STD;
//    can1Filter[1].reg.list16.RTR1   = CAN_RTR_DATA;
//    can1Filter[1].reg.list16.StdID1 = 0x324;
//    can1Filter[1].reg.list16.IDE2   = CAN_ID_STD;
//    can1Filter[1].reg.list16.RTR2   = CAN_RTR_DATA;
//    can1Filter[1].reg.list16.StdID2 = 0x304;
//    can1Filter[1].reg.list16.IDE3   = CAN_ID_STD;
//    can1Filter[1].reg.list16.RTR3   = CAN_RTR_DATA;
//    can1Filter[1].reg.list16.StdID3 = 0x354;
//    can1Filter[1].reg.list16.IDE4   = CAN_ID_STD;
//    can1Filter[1].reg.list16.RTR4   = CAN_RTR_DATA;
//    can1Filter[1].reg.list16.StdID4 = 0x344;
    CAN_FilterInit(CAN1, can1Filter, (uint16_t)(sizeof(can1Filter)/sizeof(FilterUnit)));
    /* 底盘CAN2过滤器初始化 */
    can2Filter[0].mode              = MODE_LIST16;
    can2Filter[0].FilterFIFO        = CAN_FILTER_FIFO0;
    can2Filter[0].reg.list16.IDE1   = CAN_ID_STD;
    can2Filter[0].reg.list16.RTR1   = CAN_RTR_DATA;
    can2Filter[0].reg.list16.StdID1 = 0x320;
    can2Filter[0].reg.list16.IDE2   = CAN_ID_STD;
    can2Filter[0].reg.list16.RTR2   = CAN_RTR_DATA;
    can2Filter[0].reg.list16.StdID2 = 0x310;
    can2Filter[0].reg.list16.IDE3   = CAN_ID_STD;
    can2Filter[0].reg.list16.RTR3   = CAN_RTR_DATA;
    can2Filter[0].reg.list16.StdID3 = 0x311;
    can2Filter[0].reg.list16.IDE4   = CAN_ID_STD;
    can2Filter[0].reg.list16.RTR4   = CAN_RTR_DATA;
    can2Filter[0].reg.list16.StdID4 = 0x321;

    can2Filter[1].mode              = MODE_LIST16;
    can2Filter[1].FilterFIFO        = CAN_FILTER_FIFO0;
    can2Filter[1].reg.list16.IDE1   = CAN_ID_STD;
    can2Filter[1].reg.list16.RTR1   = CAN_RTR_DATA;
    can2Filter[1].reg.list16.StdID1 = 0x355;
    can2Filter[1].reg.list16.IDE2   = CAN_ID_STD;
    can2Filter[1].reg.list16.RTR2   = CAN_RTR_DATA;
    can2Filter[1].reg.list16.StdID2 = 0x304;
    can2Filter[1].reg.list16.IDE3   = CAN_ID_STD;
    can2Filter[1].reg.list16.RTR3   = CAN_RTR_DATA;
    can2Filter[1].reg.list16.StdID3 = 0x206;
    can2Filter[1].reg.list16.IDE4   = CAN_ID_STD;
    can2Filter[1].reg.list16.RTR4   = CAN_RTR_DATA;
    can2Filter[1].reg.list16.StdID4 = 0x201;


    CAN_FilterInit(CAN2, can2Filter, (uint16_t)(sizeof(can2Filter)/sizeof(FilterUnit)));
}

void Modules_Init(void)
{
	Gimbal_Init();		//云台初始化
	Emission_Init();	//发射初始化
}

